package pnu.ssm.sbchild;

import pnu.ssm.chinese.ChineseHelper;
import pnu.ssm.drawchinese.DrawMainBoard;
import pnu.ssm.http.HttpConnector;
import pnu.ssm.key.Key;
import pnu.ssm.key.SBString;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChineseActivity extends Activity implements DrawMainBoard.NextChineseListener{
	
	public static final String TAG = "ChineseActivity";
	public static final int ID_ROOT = 0;
	public static final int ID_DRAW_MAIN_BOARD = 1;
	
	DrawMainBoard drawMainBoard;
	Button btnBoardMainReset;
	ProgressDialog progressDialog;
	ChineseHelper chineseHelper;
	SharedPreferences pref;
	SharedPreferences.Editor editor;
	HttpConnector httpConnector ;
	TextView tv_meaning;
	TextView tv_level;
	Thread chineseThread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_chinese);
		buildComponents();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == ChoiceActivity.CHOICE_ACTIVITY){
			if(resultCode == ChoiceActivity.SUCCESS){
				pref = getPreferences(MODE_PRIVATE);
				editor = pref.edit();

				String child_num = pref.getString(Key.child_num, "0");
				String extra_time = pref.getString(Key.extra_time, "5"); // 한문제당 얻는 시간 (고정)
				String obtain_time = pref.getString(Key.obtain_time, "0");
				
				int obtain_time_int = Integer.parseInt(extra_time) + Integer.parseInt(obtain_time);
				editor.putString(Key.obtain_time, String.valueOf(obtain_time_int));
				editor.commit();
				
				chineseThread = new Thread(new ChineseTask(child_num, obtain_time));
				chineseThread.start(); //sending..
				showDialog(true,extra_time);
			}else{
				showDialog(false, "");
			}
		}
		Log.e(TAG, "here is correct!!!!!");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		drawMainBoard.closeBitmaps();
	}
	
	private void buildComponents(){
		DrawMainBoard.setOnNextChineseListener(this);
		pref = getPreferences(MODE_PRIVATE);
		editor = pref.edit();
		httpConnector = HttpConnector.getInstance();
		chineseHelper = ChineseHelper.getInstance();
		chineseHelper.setContext(this);
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(SBString.WAIT);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		
		tv_meaning = (TextView) findViewById(R.id.tv_curr_meaning);
		tv_level = (TextView) findViewById(R.id.tv_curr_level);
		
		btnBoardMainReset = (Button) findViewById(R.id.btn_board_main_reset);
		btnBoardMainReset.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		LinearLayout chineseRL = (LinearLayout) findViewById(R.id.ll_chinese_board);
		//Draw Board를 추가하여 사용자가 그림을 그릴 수 있도록 합니다.
		drawMainBoard = new DrawMainBoard(this);
		drawMainBoard.setId(ID_DRAW_MAIN_BOARD);
		LinearLayout.LayoutParams linearParamsDrawMainBoard = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		linearParamsDrawMainBoard.setMargins(10, 10, 10, 25);
		drawMainBoard.setBackgroundResource(R.drawable.border_chinese);
		chineseRL.addView(drawMainBoard,linearParamsDrawMainBoard);
	}

	@Override
	public void nextChinese(final String meaning, final String level) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(tv_meaning == null || tv_level == null){
					return;
				}
				tv_meaning.setText(meaning);
				tv_level.setText(level+"급");
			}
		});
	}
	
	class ChineseTask implements Runnable{
		String child_num;
		String obtain_time;
		
		public ChineseTask(String child_num, String obtain_time) {
			this.child_num = child_num;
			this.obtain_time = obtain_time;
		}
		@Override
		public void run() {
			httpConnector.sendChineseState(child_num, obtain_time);
		}
	}
	
	private void showDialog(boolean success, String extra_time){
		String msg = "";
		if(success){
			msg = extra_time+"분을 획득했습니다.";
		}else{
			msg = "틀렸네요. 다음 기회에...";
		}
		
	    AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
	    alt_bld.setMessage(msg)
	    .setCancelable(false)
	    .setPositiveButton("확인",new DialogInterface.OnClickListener() {
	        @Override
			public void onClick(DialogInterface dialog, int id) {
	        }});
	    AlertDialog alert = alt_bld.create();
	    alert.setTitle("한석봉 잠금");
	    alert.setIcon(R.drawable.ic_launcher);
	    alert.show();
	}
}
